package qq2564874169.tuaotuao.fx.orm;


import qq2564874169.tuaotuao.fx.Action;
import qq2564874169.tuaotuao.fx.EventDispatcher;

import java.util.Map;

public interface SqlCore extends AutoCloseable {
    Map<String, Object> userdata();

    void changeDataSource(String key);

    String paramName(String name);

    String escapeField(String field);

    @Override
    void close();

    boolean isClosed();

    EventDispatcher<TransFinishArgs> evTransactionBefore();

    EventDispatcher<TransFinishArgs> evTransactionAfter();

    //todo 没有执行过增删改操作则可以直接关闭事务
    Transaction transaction(TransactionLevel level);

    boolean inTransaction();

    default Transaction transaction() {
        return transaction(TransactionLevel.Default);
    }

    default void transaction(Action action) {
        transaction(null, action);
    }

    default void transaction(TransactionLevel level, Action action) {
        try (Transaction tx = transaction(level)) {
            action.execute();
            tx.commit();
        }
    }
}
